<template>
  <el-card>
    <el-button
      @click="handleAdd"
      style="float: right; padding: 3px 0"
      type="text"
      @close="brushPage"
      >新增用户</el-button
    >
    <el-dialog title="新增用户" :visible.sync="dialogFormVisible" width="30%">
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input v-model="form.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input
            v-model="form.password"
            show-password
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth" prop="role">
          <el-select v-model="form.role" placeholder="请选择身份">
            <el-option label="管理员" value="1"></el-option>
            <el-option label="普通用户" value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addUser('form')">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="创建时间" width="200">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 10px">{{ scope.row.creationDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="姓名" width="180">
        <template slot-scope="scope">
          <el-popover trigger="hover" placement="top">
            <p>姓名: {{ scope.row.username }}</p>
            <p>身份: {{ scope.row.userRole === 2 ? "普通用户" : "管理员" }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.username }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      title="修改用户"
      :visible.sync="dialogFormVisible2"
      width="30%"
      @close="brushPage"
    >
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item
          label="用户名"
          :label-width="formLabelWidth"
          prop="username"
        >
          <el-input
            v-model="form.username"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item
          label="密码"
          :label-width="formLabelWidth"
          prop="password"
        >
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="权限" :label-width="formLabelWidth" prop="role">
          <el-select v-model="form.role" placeholder="请选择身份">
            <el-option label="管理员" :value="1"></el-option>
            <el-option label="普通用户" :value="2"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="modifyUser('form')">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      form: {
        username: "",
        password: "",
        role: "",
      },
      dialogFormVisible: false,
      dialogFormVisible2: false,
      formLabelWidth: "80px",
      currentRow: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
        role: [{ required: true, message: "请选择权限", trigger: "change" }],
      },
    };
  },
  methods: {
    handleAdd() {
      this.dialogFormVisible = true;
    },
    handleEdit(index, row) {
      this.dialogFormVisible2 = true;
      this.form.role = row.userRole;
      this.form.username = row.username;
      this.form.password = row.password;
    },

    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该用户, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$store.dispatch("deleteAccout", row);
          setTimeout(() => {
            location.reload();
          }, 400);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addUser(form) {
      this.dialogFormVisible = false;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$store.dispatch("addUser", this.form);
          setTimeout(() => {
            location.reload();
          }, 800);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    modifyUser(form) {
      this.dialogFormVisible2 = false;
      this.$refs[form].validate((valid) => {
        if (valid) {
          this.$store.dispatch("modifyUser", this.form);
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    brushPage() {
      setTimeout(() => {
        location.reload();
      }, 400);
    },
  },
  computed: {
    ...mapState({
      tableData: (state) => state.home.userList,
    }),
  },
  mounted() {
    this.$store.dispatch("userInfo");
  },
};
</script>

<style lang="less" scoped>
.el-card {
  margin-top: 30px;
  width: 600px;
}
.el-input {
  width: 220px;
}
</style>
